package com.easy.base.api.advice;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.easy.base.api.model.dto.ResultBody;
import com.easy.base.api.model.exception.BizException;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@ControllerAdvice
public class BizExceptionHandler {

    @Resource
    private HttpServletRequest request;

    @ExceptionHandler(value = BizException.class)
    @org.springframework.web.bind.annotation.ResponseBody
    public ResponseEntity<ResultBody<Object>> errorHandler(HttpServletRequest request, BizException e) {
        log.error(e.getMessage());
        ResultBody<Object> body = e.getBody();
        return ResponseEntity.status(e.getStatus())
                .body(body);
    }
}
